import { getStorageItem } from '../utils/storage';

Component({
  data: {
    hoverVisible: true,
    chaX: 0, // 转换值X
    chaY: 0, // 转换值Y
    touch: false, // 触摸标记
    posX: 0, // 初始位置
    posY: 0, // 初始位置
    show: true,
    selected: 0,
    color: '#666666',
    selectedColor: '#EB133C',
    list: [
      {
        pagePath: 'pages/index/index',
        text: '首页',
        iconPath: '/assets/image/tabBar/home_icon.png',
        selectedIconPath: '/assets/image/tabBar/home_icon_active.png',
      },
      // {
      //   pagePath: 'pages/index/videoTeach/videoList',
      //   text: '教程',
      //   iconPath: '/assets/image/tabBar/course.png',
      //   selectedIconPath: '/assets/image/tabBar/course-active.png',
      // },
      {
        pagePath: 'pages/inviteTest/inviteTest',
        text: '邀请',
        iconPath: '/assets/image/tabBar/invite.png',
        selectedIconPath: '/assets/image/tabBar/invite_active.png',
      },
      // 添加中心按钮时参考以下内容，需要添加isCenter: true
      // {
      //   pagePath: 'pages/202510activity/index',
      //   text: '',
      //   iconPath: '/assets/image/tabBar/202510activity-icon.png',
      //   selectedIconPath: '/assets/image/tabBar/202510activity-icon.png',
      //   isCenter: true,
      // },
      {
        pagePath: 'pages/hb/hb',
        text: '红包',
        iconPath: '/assets/image/tabBar/hb_icon.png',
        selectedIconPath: '/assets/image/tabBar/hb_icon_active.png',
      },
      {
        pagePath: 'pages/home/home',
        text: '我的',
        iconPath: '/assets/image/tabBar/my_icon.png',
        selectedIconPath: '/assets/image/tabBar/my_icon_active.png',
      },
    ],
    tql: false,
    token: '',
  },
  attached() {
    let { safeArea, screenWidth } = getApp().globalData.deviceInfo;
    let token = getStorageItem('token');
    // const tmpList = this.data.list;
    // if (getStorageItem('id') !== '6cabbf07fe484b3ca41c683c2f323a3b') {
    //   tmpList.splice(1, 0, {
    //       pagePath: 'pages/index/videoTeach/videoList',
    //       text: '教程',
    //       iconPath: '/assets/image/tabBar/course.png',
    //       selectedIconPath: '/assets/image/tabBar/course-active.png',
    //   })
    // }
    this.setData({
      token,
      posX: screenWidth - 80,
      posY: safeArea.screenHeight - 220,
      // list: tmpList
    });
  },
  lifetimes: {
    ready() {
      this.setTabBar();
    },
  },
  methods: {
    closeHover() {
      this.setData({
        hoverVisible: false,
      });
    },
    judgeShow() {
      if (!getStorageItem('token')) {
        this.setData({
          tql: true,
        });
        return '';
      }
    },
    watch(oldVal, newVal) {
      this.changeShow(newVal);
    },
    changeShow(show) {
      this.setData({
        show: show,
      });
    },
    setTabBar() {
      const pages = getCurrentPages();
      const pageName = pages[pages.length - 1].route;
      this.setData({
        selected: this.data.list.findIndex((item) => item.pagePath === pageName),
      });
    },
    switchTab(e) {
      const data = e.currentTarget.dataset;

      const url = data.path;
      wx.switchTab({
        url: `/${url}`,
        fail: (err) => {
          console.warn(err);
        },
      });
    },
  },
});
